Ext.define('cloud.controller.Sysfunction', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            sysfunction: 'sysfunction',
            sysfunctionedit: 'sysfunctionedit',
            sysfunctionadd: 'sysfunctionadd',
            selectID: 'sysfunction #SysfunctionList',
            search_sysfunction: 'sysfunction #search_sysfunction',
            btn_add: 'sysfunction #sys_add',
            addSubmit: 'sysfunctionadd #sys_submit',
            submit_edit: 'sysfunctionedit #sys_edit'
        },
        control: {
            /**
             * search app
             */
            search_sysfunction: {
                change: function () {
                    var Sysfunction = Ext.getStore('Sysfunction');
                    var value = this.getSearch_sysfunction().getValue();
                    Sysfunction.clearFilter();
                    if (value != "") {
                        Sysfunction.filter('name', value, true);
                    }
                    Sysfunction.load();
                }
            },
            /**
             * show appadd page
             */
            btn_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask' });
                    var sysfunctionpid = Ext.getStore('Sysfunctionpid');
                    sysfunctionpid.load({callback: function (data) {
                        var sysfunction = this.getSysfunction();
                        var sysfunctionadd = this.getSysfunctionadd()||Ext.create('cloud.view.sysfunction.SysfunctionAdd');
                        data.push({id: '0', name: '请选择'});
                        Ext.getCmp('sysfunctionpid').setOptions(data);
                        sysfunction.push(sysfunctionadd);
                        Ext.Viewport.unmask();
                    }, scope: this});

                }
            },
            /**
             * show appshow(or edit) page
             */
            selectID: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask' });
                    var sysfunctionpid = Ext.getStore('Sysfunctionpid');
                    sysfunctionpid.load({callback: function (data) {
                        var sysfunction = this.getSysfunction();
                        var sysfunctionedit = this.getSysfunctionedit();
                        if (sysfunctionedit == null) {
                            sysfunctionedit = Ext.create('cloud.view.sysfunction.SysfunctionEdit');
                        }
                        data.push({id: '0', name: '请选择'});
                        Ext.getCmp('sysfunctionpid').setOptions(data);
                        Ext.getCmp('sys_function_ID').setValue(record.get('id'));
                        sysfunctionedit.setRecord(record);
                        sysfunction.push(sysfunctionedit);
                        Ext.Viewport.unmask();
                    }, scope: this});

                }
            },
            /**
             * insert app
             */
            addSubmit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getSysfunctionadd().submit({
                        url: GLOBAL.api_url + 'function_add',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * update app
             */
            submit_edit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getSysfunctionedit().submit({
                        url: GLOBAL.api_url + 'function_update',
                        params: {
                            id: Ext.getCmp('sys_function_ID').getValue()
                            },
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * delete app
             */
            submit_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getAppshow().submit({
                                url: GLOBAL.api_url + 'app_update&disabled=true',
                                scope: this,
                                method: 'POST',
                                success: this.executeSuccess
                            });
                        }
                    }, this);
                }
            }
        }
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('Sysfunction').load();
            this.getSysfunction().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
});